import { Settings } from './../../providers/settings';
import { LoginPage } from './../login/login';
import { Component, ViewChild } from '@angular/core';
import { App, NavController, NavParams, Events } from 'ionic-angular';

/*
  Generated class for the Me page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  selector: 'page-me',
  templateUrl: 'me.html',
  providers:[Settings]    
})
export class MePage {
  loginUsr:string;
  isLogined:boolean=false;
  @ViewChild("mainNav") nav:NavController;
  constructor(public navCtrl: NavController, 
  public navParams: NavParams,
  public appctl:App,
  private settings:Settings,
  public events:Events) {
    
  }
  listenEvents(){
    this.events.subscribe("user:login",(data)=>{
      // this.appctl.getRootNav().pop();
      this.loadLoginInfo()
    })
  }
  ionViewWillUnload(){
    console.log('ionViewWillUnload MePage');
  }
  ionViewWillLeave(){
      console.log('ionViewWillLeave MePage');
  }
  ionViewDidLeave(){
    console.log('ionViewDidLeave MePage');
  }
  ionViewDidEnter(){
    console.log('ionViewDidEnter MePage');
  }
  ionViewDidLoad() {
    this.listenEvents();
    this.events.subscribe("begintest",_=>{
      console.log("trigger begintest in MePage")
    })    
    
    console.log('ionViewDidLoad MePage');
  }
  ionViewWillEnter(){
    
    this.loadLoginInfo();
  }
  loadLoginInfo(){
    console.log('ionViewWillEnter MePage');
    let disNo=this.settings.Get("disNo");
    if(!disNo){
      this.loginUsr="未登录";
    }else{
      this.loginUsr=disNo;
      this.isLogined=true;
    }
  }
  login(){
    this.events.publish("begintest");
    this.appctl.getRootNav().push(LoginPage);
  }
  logout(){
    this.settings.Delete("disNo");
    this.isLogined=false;
    this.loginUsr="未登录";
  }
}
